<?php

class UrlHelper
{
    private function __construct() {}

    public static function GetAbsolutePath( $path )
    {
        if ( !empty($path) && !self::IsPathAbsolute($path) && $path[0] != '/' )
        {
            $path = '/'.$path;
        }
        return $path;
    }

    public static function IsPathAbsolute( $path )
    {
        return !empty($path) 
        	&& (strpos($path, 'http://') === 0
             || strpos($path, 'https://') === 0);
    }

    public static function HasParams($pageUrl)
    {
        $result = FALSE;

        $urlSpluitted = explode('?', $pageUrl);

        if ($urlSpluitted && count($urlSpluitted) > 1)
        {
            $result = TRUE;
        }
        return $result;
    }

    public static function ParamsMatched($url1, $url2)
    {
        $nParams = 0;

        $url1Params = self::GetParams($url1);
        $url2Params = self::GetParams($url2);

        if(is_array($url1Params) && is_array($url2Params))
        {
            foreach($url1Params as $pName => $pValue)
            {
                if (isset($url2Params[$pName]) && $url2Params[$pName] == $pValue)
                {
                    $nParams++;
                }
            }
        }

        return $nParams;
    }

    public static function SplitParams($pageUrl, &$url, &$params = null)
    {
        $params = null;

        $urlInfo = parse_url($pageUrl);
        if ( $urlInfo && isset($urlInfo['path']) )
        {
            $url = urldecode( $urlInfo['path'] );
            $url = trim( $url );
            $urlParams  = isset($urlInfo['query']) ? $urlInfo['query'] : '';
        }
        else
        {
            list($url, $urlParams) = Util::Split('?', $pageUrl, 2);
        }
        parse_str($urlParams, $params);
    }

    public static function GetParams($pageUrl)
    {
        $url    = NULL;
        $params = NULL;

        self::SplitParams($pageUrl, $url, $params);

        return $params;
    }
    
    public static function GetHost($url)
    {
        return parse_url( $url, PHP_URL_HOST );
    }
    
    public static function GetScheme($url)
    {
        return parse_url( $url, PHP_URL_SCHEME );
    }

    public static function AddUrlParams($url, $params = array())
    {
        if (empty($params))
        {
            return $url;
        }

        $separator = '?';
        if ( strpos($url, '?') !== false )
        {
            $separator = '&';
        }
        $insertPos = strpos($url, '#');
        if ( $insertPos === false )
        {
            $insertPos = strlen($url);
        }

        $params = (array)$params;
        $paramsStr = array();
        foreach ($params as $key => $value)
        {
            $paramsStr[] = $key . '=' . $value;
        }
        $paramsStr = $separator . implode('&', $paramsStr);

        $newUrl = substr_replace($url, $paramsStr, $insertPos, 0);
        
        return $newUrl;
    }

    public static function RemoveUrlParams($url, array $params)
    {
        $newParams = array();
        $urlParts = explode('?', $url);
        if ( isset($urlParts[1]))
        {
            $queryString = $urlParts[1];
            parse_str($queryString, $urlParams);
            $newParams = array_diff_key($urlParams, array_fill_keys($params, 0));
            $url = !empty($newParams) ? $urlParts[0] . '?' . http_build_query($newParams, '', '&') : $urlParts[0];
        }

        if (empty($params))
        {
            $url = $urlParts[0];
        }
        
        return $url;
    }
}

?>